<?
require("required/dbconnect.php");
require("required/share_function.php");



if (!empty($_GET['product_name'])) {
    $product_name = urldecode($_GET['product_name']);

    $get_id = mysql_query("select * from os_products where clean_url='$product_name'") or die(mysql_error());
}

if (!empty($_GET['id'])) {
    $id = $_GET['id'];
    $get_id = mysql_query("select * from os_products where id='$id'") or die(mysql_error());
}



if (mysql_num_rows($get_id) == 0) {
    header("Location:os_invalid.php?error=5");
}


$a = mysql_fetch_array($get_id, MYSQL_ASSOC);
$id = $a['id'];
$get_sub_cat = mysql_query("select * from os_sub_category where id=" . $a['sub_cat_id']);
$cat_id = $a['sub_cat_id'];
$get_cat = mysql_query("select * from os_category where id=" . $a['cat_id']);
$total_view = mysql_query("update os_products set total_view=total_view+1 where id=$id");
if (mysql_num_rows($get_sub_cat) == 0) {
    $sub_cat = "Unknown";
} else {
    $sub_cat = mysql_result($get_sub_cat, 0, "name_eng");
}

if (mysql_num_rows($get_cat) == 0) {
    $main_cat = "Unknown";
    $main_cat_id = "0";
} else {
    $main_cat = mysql_result($get_cat, 0, "name_eng");
    $main_cat_id = mysql_result($get_cat, 0, "id");
}



$get_pre_product = mysql_query("select id,product_name_eng,clean_url from os_products where id>" . $a['id'] . " and sub_cat_id=" . $a['sub_cat_id'] . " order by id asc limit 1");

$get_next_product = mysql_query("select id,product_name_eng,clean_url from os_products where id<" . $a['id'] . " and sub_cat_id=" . $a['sub_cat_id'] . " order by id desc limit 1");

$title = $a['product_name_eng'];


$get_share_pic = mysql_query("select * from os_productpic where product_id=" . $id . " order by order_id asc limit 1");
$share_title = $title;
if (mysql_num_rows($get_share_pic) == 0) {
    $pic = "empty";
    $share_piclink = WESITE_URL . "productpic/thumbnails/noimage.jpg";
    $fix = "";
} else {
    $pic = mysql_fetch_array($get_share_pic, MYSQL_ASSOC);
    $share_piclink = WESITE_URL . "productpic/thumbnails/" . $pic['name'];
    if ($pic["width"] - $pic["height"] > 0) {
        $fix = "width=180";
    } else {
        $fix = "height=130";
    }
}



require("required/header.php");
?>
<link href="facefiles/facebox.css" media="screen" rel="stylesheet" type="text/css" />
<script src="facefiles/facebox.js" type="text/javascript"></script>

<script type="text/javascript">
    jQuery(document).ready(function($) {
        $('a[rel*=facebox]').facebox() 
    })
</script>
<script src="required/js/goAjax.js" type="text/JavaScript"></script>

<table width="100%" cellpadding="5" cellspacing="0">

    <tr>

        <Td class="page_location" colspan="2">Your Location : <a href="index.php">Home</a>  > <a href="os_products.php?pid=<?= $main_cat_id ?>"><?= $main_cat ?></a> > <a href="os_view_product.php?id=<?= $a['sub_cat_id'] ?>"><?= $sub_cat ?></a> > <?= $a['product_name_eng'] ?> </Td>

    </tr>
    <Tr>
        <Td width="20%" valign="top">
            <? require("required/product_category.php"); ?>
        </Td>
        <td width="80%" valign="top">

            <table width="100%" cellpadding="0">
                <tr>

                    <td>
                        <?
                        $history_saved = 0;
                        if ($id != "") {
                            if (!empty($_SESSION['view_history'])) {
                                foreach ($_SESSION['view_history'] as $historyItems) {
                                    if ($historyItems['id'] == $id) {
                                        $history_saved = 1;
                                        break;
                                    }
                                }
                            } else {
                                $history_saved = 0;
                            }


                            if ($history_saved == 0) {

                                $_SESSION['view_history'][] = array(
                                    "id" => $id,
                                    "time" => time()
                                );
                            }
                        }
                        ?>
                        <table width="100%">
                            <tr>
                                <td width="50%" align="left">
                                    <?
                                    if (mysql_num_rows($get_pre_product) > 0) {
                                        $pre = mysql_fetch_array($get_pre_product, MYSQL_ASSOC);
                                        ?>
                                        <a href="<?= getProductUrl($pre['id'], $pre['clean_url'], $module_url_rewrite) ?>" class="button" >Previous Product</a>

                                    <? } ?>
                                </td>
                                <td width="50%" align="right">
                                    <?
                                    if (mysql_num_rows($get_next_product) > 0) {
                                        $next = mysql_fetch_array($get_next_product, MYSQL_ASSOC);
                                        ?>
                                        <a href="<?= getProductUrl($next['id'], $next['clean_url'], $module_url_rewrite) ?>" class="button" >Next Product</a>

                                    <? } ?>
                                </td>
                            </tr>
                        </table>

                    </td>
                </tr>

                <tr>
                    <td width="100%" colspan="2" valign="top">
                        <?
                        if (!empty($_GET['error'])) {
                            if ($_GET['error'] == 1) {
                                ?>
                                <div class="dw_failed">Sorry, the quantity that you have requested is not available.</div>
                                <?
                            } elseif ($_GET['error'] == 2) {
                                ?>
                                <div class="dw_failed">Sorry, You do not have enough points to redeem this product.</div>
                                <?
                            }
                        }
                        ?>
                        <table width="100%" cellpadding="0" cellspacing="0">
                            <tr>
                                <td valign="top" width="250">

                                    <table align="center">

                                        <tr>
                                            <Td valign="top">
                                                <table width="100%">

                                                    <?
                                                    $get_photo = mysql_query("select * from os_productpic where product_id='$id' order by order_id asc");

                                                    if (mysql_num_rows($get_photo) != 0) {
                                                        $row = 0;
                                                        $secondrow1 = "";
                                                        $secondrow2 = "";
                                                        $isFirstRow = 1;
                                                        while ($photo = mysql_fetch_array($get_photo, MYSQL_ASSOC)) {
                                                            $piclink = $photo["name"];
                                                            if ($photo["width"] - $photo["height"] > 0) {
                                                                $fix = "width=80";
                                                            } else {
                                                                $fix = "height=80";
                                                            }


                                                            if ($row % 2 == 0) {
                                                                $secondrow1 = "<tr>";
                                                                $secondrow2 = "</tr>";
                                                                $isFirstRow++;
                                                            } else {
                                                                $secondrow1 = "";
                                                                $secondrow2 = "";
                                                            }
                                                            $photo_id = $photo['id'];
                                                            $image_link = ROOT_PATH . "productpic/thumbnails/" . $piclink;
                                                            ?>
                                                            <?= $secondrow1 ?>
                                                            <Td align="center" valign="top">
                                                                <?
                                                                $image_link_large = ROOT_PATH . "productpic/" . $piclink;
                                                                ?>

                                                                <a href="<?= $image_link_large ?>" rel="facebox">
                                                                    <img src='required/thumbnail_resize.php?width=120&height=120&cropratio=1:1&image=<?= $image_link ?>' class='product_thumbnail_frame' />
                                                                </a>
                                                            </td>

                                                            <?
                                                            $row++;
                                                        }
                                                        ?>
                                                        <?= $secondrow2; ?>
                                                        <?
                                                    }
                                                    ?>


                                                </table>
                                            </Td>
                                        </tr>	
                                    </table>



                                </td>

                                <td valign="top">
                                    <table width="100%">
                                        <tr>
                                            <td  colspan="3">
                                                <table cellpadding="0" cellspacing="0" width="100%">
                                                    <Td class="product_title" width="40%">                     
                                                        <?= $a['product_name_eng'] ?>
                                                        <br>
                                                        <!-- AddThis Button BEGIN -->
                                                        <div class="addthis_toolbox addthis_default_style ">
                                                            <a class="addthis_button_facebook_like" fb:like:layout="button_count"></a>
                                                            <a class="addthis_button_tweet"></a>
                                                            <a class="addthis_button_google_plusone" g:plusone:size="medium"></a>
                                                        </div>
                                                        <script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#pubid=ra-4ed84d7d219ee758"></script>
                                                        <!-- AddThis Button END -->

                                                    </Td>
                                                </table>
                                            </td> 
                                        </tr>	



                                        <?
                                        if ($a['youtube_id'] != "") {
                                            ?>
                                            <Tr valign="top">
                                                <Td><strong>Related video</strong></Td>
                                                <td>:</td>
                                                <td><div style="position: absolute; visibility: hidden;float:left" id="divVideo"></div>
                                                    <a href="javascript:showFloatContent('os_ajax_video_view.php?id=<?= $a['id'] ?>','divVideo','500','50',0,1)"><img src='http://img.youtube.com/vi/<?= trim($a['youtube_id']) ?>/1.jpg'></a>
                                                    <br />
                                                    (Click to watch)
                                                    <?
                                                }
                                                ?>
                                            </td>
                                        </Tr>
                                        <tr valign="top">
                                            <td valign="top" colspan="4"><br>

                                                <?= $a['description_eng'] ?>
                                                <br><br>

                                            </td>
                                        </tr>
                                        <tr>
                                            <td width="15%" colspan="4"><strong>Product code</strong> : <?= $a['product_code'] ?><br></br>
                                                <strong>Product Weight</strong> : <?= $a['product_weight'] ?> KG
                                            </td>
                                        </tr>

                                        <script>
                                        
                                            function showStock(id)
                                            {                              
                                                $.ajax({
                                                    type: "GET",
                                                    url: 'required/js/ajax_showstock.php',
                                                    data: "id=" + id, 
                                                    success: function(data) 
                                                    {	  
                                                        $('#divItems').html(data);
                                                        setTimeout('updateTotal()',500);
                                                    }
							
                                                });
							 
							  
							  
                                            }
                          
                                            function updateTotal()
                                            {
                                                if(!isInteger(document.getElementById('quantity').value))
                                                {
                                                    alert("Please enter integer only");
                                                    return false;
                                                }else{
                                                    
                                                
                                                    var quantity=parseFloat(validateInteger(document.getElementById('quantity').value));
                                                    var product_price=parseFloat(validateInteger(document.getElementById('hid_product_price').value))*quantity;
                                                
                                                
                                                    var add_on_price_total=0;
							 
                                                    var add_on_unit_price=0;
                                                    var add_on_id=0;
							  	
                                                    if(document.frm.addon_id)
                                                    {
                                                        var boxes = document.frm.addon_id.length;
								 
                                                        if(boxes==undefined)boxes=1;
								  
                                                        if(boxes==1)
                                                        {
                                                            if (document.frm.addon_id.checked == true )
                                                            {
                                                                add_on_id=document.frm.addon_id.value;
											
                                                                add_on_unit_price=parseFloat(document.getElementById("add_on_price_"+add_on_id).value)*quantity;
											
											
                                                                add_on_price_total=add_on_price_total+add_on_unit_price;									
											
                                                            }   
                                                        }
                                                        else
                                                        {
                                                            for (i = 0; i < boxes; i++) 
                                                            {
                                                                if (document.frm.addon_id[i].checked) 
                                                                {
                                                                    add_on_id=document.frm.addon_id[i].value;
											
                                                                    add_on_unit_price=parseFloat(document.getElementById("add_on_price_"+add_on_id).value)*quantity;
											
											
                                                                    add_on_price_total=add_on_price_total+add_on_unit_price;									
                                                                }
										
                                                            }
                                                        }
                                                        product_price=product_price+add_on_price_total;
                                                        document.getElementById('divTotalAll').innerHTML="<font class='pricetag'><?= $_SESSION['currency_code'] ?> "+formatNumber(product_price,2,".")+"</font>";
                                                    }
                                                }
							 
                                            }
						  
                                            function validateFrm(frm)
                                            {
                                                var a=document.frm.quantity.value;
                            
                                                if(!isInteger(a))
                                                {
                                                    alert("Please enter integer only");
                                                    return false;
                                                }
                                                return true;
                              
                                            }
                          
                                            function isEmpty(s)
                                            {
                                                return ((s == null) || (s.length == 0))
                                            }
                      
                                            function isDigit (c)
                                            {
                                                return ((c >= "0") && (c <= "9"))
                                            }
    
                                            function isInteger (s)
                                            {
                                                var i;
                      
                                                if (isEmpty(s))
                                                    if (isInteger.arguments.length == 1) return 0;
                                                else return (isInteger.arguments[1] == true);
                      
                                                for (i = 0; i < s.length; i++)
                                                {
                                                    var c = s.charAt(i);
                      
                                                    if (!isDigit(c)) return false;
                                                }
                      
                                                return true;
                                            }
                                            
                                            function showAddonDetails(pid)
                                            {
                                                $.colorbox({
                                                    height:600,
                                                    width:600,
                                                    href:"required/js/ajax_addon_details.php?id="+pid								
                                                   									
                                                });
                                            }
                                        </script>
                                        <? ?>
                                        <tr>
                                            <td colspan="4">

                                                <?
                                                $get_stock = mysql_query("select * from os_product_inventory where pid=" . $a['id']);
                                                $initial_price = 0;
                                                if (mysql_num_rows($get_stock) > 0) {
                                                    ?>
                                                    <form action="os_addtocart.php" method="post" name="frm" onSubmit="return validateFrm(this)">
                                                        <table cellpadding="0" cellspacing="0" width="100%">

                                                            <tr valign="top">
                                                                <td	>
                                                                    <strong>Quantity </strong>  <input type="text" value="0" name="quantity" id="quantity" size="5" class="text" onKeyUp="updateTotal()">
                                                                    <?
                                                                    if ($a['show_stock_option'] == 1) {


                                                                        if ($a['stock_option_type'] == "") {
                                                                            $option_type = "Product Selection";
                                                                        } else {
                                                                            $option_type = "Select " . $a['stock_option_type'];
                                                                        }
                                                                        ?> 
                                                                        <strong><?= $option_type ?></strong> 
                                                                        <select name="item" id="item" onChange="showStock(this.value)">

                                                                            <?
                                                                            $firstItem = 0;
                                                                            $itemDesc = "";
                                                                            while ($s = mysql_fetch_array($get_stock, MYSQL_ASSOC)) {

                                                                                if ($firstItem == 0) {
                                                                                    $itemDesc = "<table>";
                                                                                    if ($s['quantity'] > 0) {

                                                                                        $itemDesc.="<td height=70 valign=top>";
                                                                                        $itemDesc.="<font class='pricetag'> " . $_SESSION['currency_code'] . " " . number_format($_SESSION['currency_rate'] * $s['unit_price'], 2) . "</font>";

                                                                                        if ($a['show_stock_quantity'] == 1) {
                                                                                            $itemDesc.="<br> <br><strong>Quantity Available : " . $s['quantity'] . "</strong><br><br>";
                                                                                        }
                                                                                    } else {
                                                                                        $itemDesc.="<td valign=top height=70 style='background-image:url(\"images/soldout.png\");background-position:0 20;background-repeat:no-repeat'>";
                                                                                        $itemDesc.="<font class='pricetag'> " . $_SESSION['currency_code'] . " " . number_format($_SESSION['currency_rate'] * $s['unit_price'], 2) . "</font>";
                                                                                    }

                                                                                    $initial_price = $s['unit_price'];
                                                                                    $itemDesc.="<input type='hidden' value='" . $s['unit_price'] . "' id='hid_product_price' name='hid_product_price'>";
                                                                                    $itemDesc.="</td></table>";
                                                                                }
                                                                                ?>
                                                                                <option value="<?= $s['id'] ?>">
                                                                                    <?= $s['description'] ?>
                                                                                </option>
                                                                                <?
                                                                                $firstItem++;
                                                                            }
                                                                            ?>
                                                                        </select>

                                                                        <?
                                                                    } else {
                                                                        $get_available_stock = mysql_query("select * from os_product_inventory where pid=" . $a['id'] . " order by quantity desc limit 1");

                                                                        $s = mysql_fetch_array($get_available_stock, MYSQL_ASSOC);
                                                                        $itemDesc = "<table>";
                                                                        if ($s['quantity'] > 0) {
                                                                            $itemDesc.="<td height=70 width='200' valign=top align='right'>";
                                                                            $itemDesc.="<font class='pricetag'> " . $_SESSION['currency_code'] . " " . number_format($_SESSION['currency_rate'] * $s['unit_price'], 2) . "</font>";


                                                                            if ($a['show_stock_quantity'] == 1) {
                                                                                $itemDesc.="<br> <br><strong>Quantity Available : " . $s['quantity'] . "</strong><br><br>";
                                                                            }
                                                                        } else {
                                                                            $itemDesc.="<td valign=top height=70 width='100' style='background-image:url(\"images/soldout.png\");background-position:center;background-repeat:no-repeat'>";
                                                                            $itemDesc.="<font class='pricetag'> " . $_SESSION['currency_code'] . " " . number_format($_SESSION['currency_rate'] * $s['unit_price'], 2) . "</font>";
                                                                        }
                                                                        $initial_price = $s['unit_price'];
                                                                        $itemDesc.="<input type='hidden' value='" . $s['id'] . "' name='item' id='item'></td></table>";
                                                                        $itemDesc.="<input type='hidden' value='" . $s['unit_price'] . "' id='hid_product_price' name='hid_product_price'>";
                                                                    }
                                                                    ?>
                                                                </td>
                                                                <Td valign="middle" align="right">
                                                                    <div id="divItems">
                                                                        <?= $itemDesc ?>

                                                                    </div>
                                                                </Td>
                                                            </tr>
                                                            <tr>
                                                            <tr>
                                                                <td colspan="4" >

                                                                    <?
                                                                    $get_add_on = mysql_query("select a.id as aid, a.cat_id, a.sub_cat_id, b.id as bid, b.product_id,b.add_on_id 
								from os_products a inner join os_product_add_on b on a.id=b.add_on_id where b.product_id='" . $a['id'] . "' order by a.cat_id asc, a.sub_cat_id asc ") or die(mysql_error());

                                                                    if (mysql_num_rows($get_add_on) > 0) {
                                                                        ?>
                                                                        <table cellpadding="2" cellspacing="1" width="100%">
                                                                            <tr >

                                                                                <Td colspan="6" class="title">Add-on</Td> 
                                                                            </tr>

                                                                            <?
                                                                            $no = 1;
                                                                            $current_cat = 0;
                                                                            while ($ao = mysql_fetch_array($get_add_on, MYSQL_ASSOC)) {
                                                                                $get_product = mysql_query("select * from os_products where id='" . $ao['add_on_id'] . "'") or die(mysql_error());

                                                                                $get_available_stock = mysql_query("select * from os_product_inventory where pid=" . $ao['add_on_id'] . " order by quantity desc limit 1") or die(mysql_error);

                                                                                $aop = mysql_fetch_array($get_product, MYSQL_ASSOC);



                                                                                if ($no % 2 == 0) {
                                                                                    $bgcolor = "#e5e5e5";
                                                                                } else {
                                                                                    $bgcolor = "#eeeeee";
                                                                                }

                                                                                if (mysql_num_rows($get_available_stock) > 0) {
                                                                                    $unit_price = number_format(mysql_result($get_available_stock, 0, "unit_price"), 2);
                                                                                } else {
                                                                                    $unit_price = "0";
                                                                                }

                                                                                $get_pic = mysql_query("select * from os_productpic where product_id=" . $ao['add_on_id'] . " order by order_id asc limit 1");

                                                                                if (mysql_num_rows($get_pic) == 0) {
                                                                                    $pic = "empty";
                                                                                    $piclink = "noimage.jpg";
                                                                                    $fix = "";
                                                                                } else {
                                                                                    $pic = mysql_fetch_array($get_pic, MYSQL_ASSOC);

                                                                                    $piclink = $pic['name'];

                                                                                    if ($pic["width"] - $pic["height"] > 0) {
                                                                                        $fix = "width=100";
                                                                                    } else {
                                                                                        $fix = "height=100";
                                                                                    }
                                                                                }



                                                                                $image_link = "productpic/thumbnails/" . $piclink;


                                                                                if ($current_cat != $aop['sub_cat_id']) {
                                                                                    $current_cat = $aop['sub_cat_id'];
                                                                                    ?>
                                                                                    <tr>
                                                                                        <Td colspan="5" class="table_header"><?= getDataById("os_sub_category", "name_eng", "id", $aop['sub_cat_id']) ?></Td>
                                                                                    </tr>
                                                                                <? } ?>
                                                                                <tr height="20" valign="top">	
                                                                                    <Td><input type="checkbox" name="addon_id" value="<?= $aop['id'] ?>" onclick="updateTotal()"/></Td>
                                                                                    <td align="center">
                                                                                        <img src='<?= $image_link ?>' <?= $fix ?> class='product_thumbnail_frame'/>
                                                                                        <br />
                                                                                    </td>
                                                                                    <td width="60%">
                                                                                        <b><?= $aop['product_name_eng'] ?></b><br />


                                                                                        <?= truncate($aop['description_eng'], 100) ?>
                                                                                        <br>
                                                                                        Product weight : <?= $aop['product_weight'] ?> KG
                                                                                        <br>
                                                                                        <a href="javascript:showAddonDetails(<?= $aop['id'] ?>)">More info</a>
                                                                                        <br /><br />
                                                                                    </td>	
                                                                                    <td class='pricetag' width="20%" align="right"> <?= $_SESSION['currency_code'] ?> <?= number_format($unit_price, 2) ?>
                                                                                        <input type="hidden" value="<?= $unit_price ?>" name="add_on_price_<?= $aop['id'] ?>" id="add_on_price_<?= $aop['id'] ?>" />
                                                                                    </td>	
                                                                                </tr>


                                                                                <?
                                                                                $no++;
                                                                            }
                                                                            ?>
                                                                            <Tr height="40">
                                                                                <Td colspan="3" align="right" style="border-top:1px solid #666;border-bottom:1px double #666"><font class="pricetag">Total</font></Td>
                                                                                <Td align="right" style="border-top:1px solid #666;border-bottom:1px double #666">
                                                                                    <div id="divTotalAll"><font class="pricetag"><?= $_SESSION['currency_code'] ?> 
                                                                                        <?= number_format($initial_price, 2) ?></font></div>
                                                                                </Td>
                                                                            </Tr>
                                                                        </table>

                                                                    <? } ?>
                                                                    <br>
                                                                </td>
                                                            </tr>

                                                            <tr>
                                                                <td colspan="3" align="center">
                                                                    <script language="javascript">
                                                                        $(document).ready(function()
                                                                        {
                                                                            $("#addWishList").colorbox({width:"400", height:"300"});
                                                                        });
                                                                    </script>
                                                                    <div id='divWishList<?= $a['id'] ?>'></div>	
                                                                    <input type="hidden" value="<?= $a['id'] ?>" name="pid" id="pid">
                                                                    <input type="button" value="Add To Cart"  class="button" onclick="doUpdateOnScreen(<?= $a['id'] ?>)"/>
                                                                    <a href="os_ajax_add_wish_list.php?id=<?= $a['id'] ?>"id="addWishList"><font class="button" >Add To Wish List</font></a>
                                                                    <input type="button" value="Back"  class="button" onclick="window.open('os_view_product.php?id=<?= $a['sub_cat_id'] ?>','_self')"/>



                                                                </td>
                                                            </tr>
                                                        </table>
                                                    </form>
                                                <? } else { ?>
                                                    <div align="center"><img src="images/soldout.png" ></div>
                                                <? } ?>
                                            </td>
                                        </tr>

                                    </table>
                                    <br />
                                    <div id='divCartUpdate'  ></div>	
                                </td>
                            </tr>

                        </table>
                    </td>
                </tr>
            </table></td>
    </tr>
</table>
<? require("required/footer.php"); ?>
